/**

 @Name：cctvcc 用户登入和注册等
 */

layui.define('form', function (exports) {
    var $ = layui.$
        , layer = layui.layer
        , laytpl = layui.laytpl
        , setter = layui.setter
        , view = layui.view
        , admin = layui.admin
        , form = layui.form;

    var $body = $('body');
    // login begin

    //更换图形验证码
    $body.on('click', '#LAY-user-get-vercode', function () {
        admin.getCodeImg();
    });

    form.render();

    document.onkeydown = function (e) { // 回车提交表单
        // 兼容FF和IE和Opera
        var theEvent = window.event || e;
        var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
        if (code == 13) {
            $('#submitBut').click();
        }
    }

    //提交
    form.on('submit(LAY-user-login-submit)', function (obj) {

        // 请求公钥
        admin.req({
            url: layui.setter.ctx + '/auth/getPublicKey/' + obj.field.username
            , type: 'get'
            , done: function (res) {
                obj.field.password = encryptRequest(obj.field.password, res.data);
                doLogin(obj);
            }
        });
    });

    function doLogin(obj) {

        //请求登入接口
        admin.req({
            url: layui.setter.ctx + '/auth/login'
            , type: 'post'
            , data: JSON.stringify(obj.field)
            , contentType: 'application/json'
            , done: function (res) {
                //请求成功后，写入 access_token
                layui.data(setter.tableName, {
                    key: setter.request.tokenName
                    , value: res.data.access_token
                });

                //登入成功的提示与跳转
                layer.msg('登入成功', {
                    icon: 1
                    , time: 1000
                }, function () {
                    location.href = '../'; //后台主页
                });
            }
            , error: function (res) {
                admin.getCodeImg();
            }
        });
    }

    // 使用jsencrypt类库加密js方法，
    function encryptRequest(data, publicKey) {
        var encrypt = new JSEncrypt();
        encrypt.setPublicKey(publicKey);
        // ajax请求发送的数据对象
        return encrypt.encrypt(data);
    }

    // login end

    // registry begin
    //提交
    form.on('submit(LAY-user-reg-submit)', function (obj) {
        var field = obj.field;

        //确认密码
        if (field.password !== field.repass) {
            return layer.msg('两次密码输入不一致');
        }

        //是否同意用户协议
        if (!field.agreement) {
            return layer.msg('你必须同意用户协议才能注册');
        }

        //请求接口
        admin.req({
            url: layui.setter.ctx + '/auth/register'
            , type: 'post'
            , data: JSON.stringify(obj.field)
            , contentType: 'application/json'
            , done: function (res) {
                layer.msg('注册成功', {
                    icon: 1
                    , time: 1000
                }, function () {
                    location.href = '../auth/login.html'; //跳转到登入页
                });
            }
        });

        return false;
    });

    //registry end

    // forget begin

    //找回密码下一步
    form.on('submit(LAY-user-forget-submit)', function (obj) {
        var field = obj.field;

        //请求接口
        admin.req({
            url: layui.setter.ctx + '/auth/forget'
            , type: 'post'
            , data: obj.field
            , done: function (res) {
                location.hash = '/type=resetpass';
                location.reload();
            }
        });

        return false;
    });

    //重置密码
    form.on('submit(LAY-user-forget-resetpass)', function (obj) {
        var field = obj.field;

        //确认密码
        if (field.password !== field.repass) {
            return layer.msg('两次密码输入不一致');
        }

        //请求接口
        admin.req({
            url: layui.setter.ctx + '/auth/resetpass'
            , type: 'put'
            , data: field
            , done: function (res) {
                layer.msg('密码已成功重置', {
                    icon: 1
                    , time: 2000
                }, function () {
                    location.href = 'login.html'; //跳转到登入页
                });
            }
            , error: function (res) {
                setTimeout(function () {
                    location.hash = '/';
                    location.reload();
                }, 3000);
            }
        });

        return false;
    });

    // forget end

    // common begin

    // 获取图形验证码
    admin.getCodeImg = function () {
        admin.req({
            url: layui.setter.ctx + '/code'
            , type: 'get'
            , done: function (res) {
                document.getElementById('LAY-user-get-vercode').setAttribute('src', 'data:image/png;base64,' + res.data.img);
                document.getElementById('LAY-user-login-vercode_id').setAttribute('value', res.data.uuid);
            }
        });
    }

    $body.on('blur', '#LAY-user-login-username-reg', function () {
        var userName = $('#LAY-user-login-username-reg').val();
        if ('' == userName)
            return;
        if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(userName))
            return;
        if (/(^\_)|(\__)|(\_+$)/.test(userName))
            return;
        //请求接口
        admin.req({
            url: layui.setter.ctx + '/auth/checkUserNameUnique/' + userName
            , type: 'get'
            , done: function (res) {
                if (layui.setter.no == res.data) {
                    $('#LAY-user-login-username-reg').val('');
                    layer.msg('用户名已存在', {icon: 5});
                }
            }
        });
    });

    //发送邮箱验证码
    admin.sendAuthCode({
        elem: '#LAY-user-getsmscode'
        , elemEmailValue: '#LAY-user-login-emailValue'
        , elemVercode: '#LAY-user-login-vercode'
        , eleEmailType: '#LAY-user-login-emailType'
        , ajax: {
            type: 'post'
            , url: layui.setter.ctx + '/auth/getEmailCode'
        }
    });

    // common end

    //对外暴露的接口
    exports('login', {});
});